<?php

namespace App\Tables;

use Illuminate\Database\Eloquent\Model;

class UserScoreTransferRecord extends Model
{
    protected $table = 'user_score_transfer_records';


    protected $fillable = ['user_id', 'type', 'order_sn', 'score', 'total_score'];

    protected $appends = ['type_text', 'username'];

    protected $hidden = [
        'user'
    ];

    /**
     * 积分变动类型
     * 变动类型 consume 消费；backend_give 后台赠送；charge_give 充值赠送；sign_in_give 签到赠送；order_give 订单消费赠送; invite_user_give 邀请新用户
     * @return string
     */
    public function getTypeTextAttribute()
    {
        switch ($this->type) {
            case 'consume':
                return '消费';
            case 'backend_give':
                return '后台赠送';
            case 'charge_give':
                return '充值赠送';
            case 'sign_in_give':
                return '签到赠送';
            case 'invite_user_give':
                return '邀请新用户赠送';
            case 'order_give':
                return '订单消费赠送';
        }

        return $this->type;
    }

    /**
     * 获取用户信息
     * @return string
     */
    public function getUsernameAttribute()
    {
        $username = $this->user->name;
        if (!empty($this->user->nickname)) {
            $username .= '/' . $this->user->nickname;
        }

        return $username;
    }

    /**
     * 获取用户详情
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
     */
    public function user()
    {
        return $this->hasOne('App\Tables\User', 'id', 'user_id');
    }
}
